<template>
  <div class="data-energy-wrapper">
    <xyDataHead :showDetail="false">
      <template #icon>
        <svg-icon :icon-class="svgIcon" class="svg-icon"></svg-icon>
      </template>
      <template #title>
        <p>{{ title }}</p>
      </template>
      <template #number>
        <div class="warn-number" v-show="num">{{ num }}</div>
      </template>
    </xyDataHead>
    <div class="data-energy-container" ref="chartContainer"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";
import xyDataHead from "./xy-data-head.vue";
export default {
  components: {
    xyDataHead,
  },
  props: {
    title: {
      type: String,
      default: "",
    },
    svgIcon: {
      type: String,
      default: "",
    },
    num: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      chart: null,
      chartData: {
        xAxis: ["1月", "4月", "7月", "10月", ""],
        // 修改为包含两个数据系列
        series1: [450, 932, 901, 934, 780],
        series2: [620, 732, 425, 734, 580],
      },
    };
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose();
      window.removeEventListener("resize", this.resizeChart);
    }
  },
  methods: {
    initChart() {
      this.$nextTick(() => {
        const chartDom = this.$refs.chartContainer;
        if (chartDom) {
          this.chart = echarts.init(chartDom);
          this.setChartOption();

          window.addEventListener("resize", this.resizeChart);
        }
      });
    },

    setChartOption() {
      const option = {
        tooltip: {
          trigger: "axis",
        },
        legend: {
          data: ["今年", "去年"], // 不包含占位符系列
          textStyle: {
            color: "#fff",
          },
        },
        grid: {
          // 控制绘图区域的位置和大小
          left: "3%",
          right: "4%",
          bottom: "3%",
          top: "20%",
          containLabel: true, // 是否包含坐标轴标签
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: this.chartData.xAxis,
          axisLabel: {
            color: "#9498a1", // X轴字体颜色
            margin: 10,
            fontSize: 11,
          },
          axisLine: {
            lineStyle: {
              color: "rgba(148, 163, 184, 0.2)",
            },
          },
        },
        yAxis: [
          // 修改为数组形式以支持双Y轴
          {
            type: "value",
            name: "能耗(kWh)",
            nameTextStyle: {
              color: "#408D49",
              padding: [0, 0, 0, 40],
              fontSize: 11,
            },
            splitLine: {
              lineStyle: {
                color: "rgba(148, 163, 184, 0.1)",
              },
            },
            axisLabel: {
              color: "#9498a1", // Y轴字体颜色
              fontSize: 11,
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: false, // 确保隐藏右侧Y轴竖线
              lineStyle: {
                color: "transparent", // 额外设置颜色为透明
              },
            },
          },
          {
            type: "value",
            name: "增长率(%)",
            nameTextStyle: {
              color: "#9498a1",
              padding: [0, 0, 0, 0],
              fontSize: 11,
            },
            splitLine: {
              show: false,
            },
            axisLabel: {
              color: "#9498a1",
              fontSize: 11,
              formatter: "{value}%",
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: false, // 确保隐藏右侧Y轴竖线
              lineStyle: {
                color: "transparent", // 额外设置颜色为透明
              },
            },
          },
        ],
        series: [
          {
            name: "今年",
            data: this.chartData.series1,
            type: "line",
            symbol: "circle",
            symbolSize: 8,
            itemStyle: {
              color: "#FF9800", // 内部实心颜色
              borderColor: "#ffffff", // 白色边框
              borderWidth: 2, // 边框宽度
              borderType: "solid", // 实线边框
            },
            lineStyle: {
              color: "#FF9800", // 线条颜色
              width: 4,
            },
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 0.8, [
                {
                  offset: 0,
                  color: "rgba(123, 135, 89, .9)",
                },
                {
                  offset: 1,
                  color: "rgba(123, 135, 89, .1)",
                },
              ]),
            },
            smooth: true,
            showSymbol: true,
          },
          {
            name: "去年",
            data: this.chartData.series2,
            type: "line",
            symbol: "circle",
            symbolSize: 8,
            itemStyle: {
              color: "#4CAF50", // 内部实心颜色
              borderColor: "#ffffff", // 白色边框
              borderWidth: 2, // 边框宽度
              borderType: "solid", // 实线边框
            },
            lineStyle: {
              color: "#4CAF50", // 线条颜色
              width: 4,
            },
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 0.5, [
                {
                  offset: 0,
                  color: "rgba(123, 135, 89, .7)",
                },
                {
                  offset: 1,
                  color: "rgba(123, 135, 89, .1)",
                },
              ]),
            },
            smooth: true,
            showSymbol: true,
          },
          // 添加透明占位系列以确保右侧Y轴显示
          {
            name: "占位符",
            type: "line",
            yAxisIndex: 1,
            data: [0, 0, 0, 0, 0],
            itemStyle: {
              opacity: 0,
            },
            lineStyle: {
              opacity: 0,
            },
            symbol: "none",
            tooltip: {
              show: false,
            },
          },
        ],
      };

      this.chart.setOption(option);
    },

    resizeChart() {
      if (this.chart) {
        // 添加延迟确保容器尺寸已更新
        setTimeout(() => {
          this.chart.resize();
        }, 100);
      }
    },
  },
  watch: {
    // 监听数据变化，更新图表
    chartData: {
      handler() {
        if (this.chart) {
          this.setChartOption();
        }
      },
      deep: true,
    },
  },
};
</script>

<style lang="scss" scoped>
.data-energy-wrapper {
  height: 100%;
  .data-energy-container {
    height: calc(100% - 50px);
    width: 100%;
    position: relative;
    margin-top: 10px;
  }
}
</style>
